/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo;


import java.io.File;
import javax.swing.JOptionPane;
import org.jgap.Chromosome;
import org.jgap.Configuration;
import org.jgap.FitnessFunction;
import org.jgap.Gene;
import org.jgap.Genotype;
import org.jgap.IChromosome;
import org.jgap.data.DataTreeBuilder;
import org.jgap.data.IDataCreators;
import org.jgap.impl.DefaultConfiguration;
import org.jgap.impl.IntegerGene;
import org.jgap.xml.XMLDocumentBuilder;
import org.jgap.xml.XMLManager;
import org.w3c.dom.Document;


public class Inventarios {

     /**
     * The total number of times we'll let the population evolve.
     */
    private static final int MAX_EVOLUCIONES_PERMITIDAS = 10;
    public static long tiempoEvolucion = 0;
    public static double cromosomaApto = 0.0;
    private static int MAX_Q=230;
    private static int MAX_r=220;
    private static int numarticulostemp;
    private double frecuenciapedidostemp;
    private double porcentajesatisfacciontemp;
    private double C1temp;
    private double C2temp;
    private double C3temp;
    private double C4temp;
    private double C5temp;
    private int Qitemp;
    private int D1temp;
    private int D2temp;
    private int D3temp;
    private int D4temp;
    private int D5temp;
    private int S1temp;
    private int S2temp;
    private int S3temp;
    private int S4temp;
    private int S5temp;
    private int E1temp=D1temp;
    private int E2temp=D2temp;
    private int E3temp=D3temp;
    private int E4temp=D4temp;
    private int E5temp=D5temp;
    private static  double[] C=new double [5];
    private static int[] D=new int [5];
    private static int[] S=new int [5];
    
    public void guardarArreglos(){
        C[0]=C1temp;
        C[1]=C2temp;
        C[2]=C3temp;
        C[3]=C4temp;
        C[4]=C5temp;
        
        D[0]=D1temp;
        D[1]=D2temp;
        D[2]=D3temp;
        D[3]=D4temp;
        D[4]=D5temp;
        
        S[0]=S1temp;
        S[1]=S2temp;
        S[2]=S3temp;
        S[3]=S4temp;
        S[4]=S5temp;
        
    }
    public static void calcular() throws Exception {
        
        
        // Se crea una configuracion con valores predeterminados.
        // -------------------------------------------------------------
        Configuration conf = new DefaultConfiguration();
        // Se indica en la configuracion que el elemento mas apto siempre pase a
        // la proxima generacion
        // -------------------------------------------------------------
        
        conf.setPreservFittestIndividual(true);
        // Se Crea la funcion de aptitud y se setea en la configuracion
        // ---------------------------------------------------------
        FitnessFunction myFunc;
        myFunc = new MinimizingQRFitnessFunction(C,D,S,numarticulostemp);
        conf.setFitnessFunction(myFunc);
        
        Gene[] GeneInv = new Gene[10];
        GeneInv[0] = new IntegerGene(conf, 0, MAX_Q); //Q1  
        GeneInv[1] = new IntegerGene(conf, 0, MAX_r); //r1 
        GeneInv[2] = new IntegerGene(conf, 0, MAX_Q); //Q2 
        GeneInv[3] = new IntegerGene(conf, 0, MAX_r); //r2 
        GeneInv[4] = new IntegerGene(conf, 0, MAX_Q); //Q3
        GeneInv[5] = new IntegerGene(conf, 0, MAX_r); //r3  
        GeneInv[6] = new IntegerGene(conf, 0, MAX_Q); //Q4 
        GeneInv[7] = new IntegerGene(conf, 0, MAX_r); //r4 
        GeneInv[8] = new IntegerGene(conf, 0, MAX_Q); //Q5 
        GeneInv[9] = new IntegerGene(conf, 0, MAX_r); //r5
        
        
        IChromosome sampleChromosome = new Chromosome(conf, GeneInv);
        conf.setSampleChromosome(sampleChromosome);
        
        
        // Por ultimo se debe indicar el tamañoo de la poblacion en la
        // configuracion
        // ------------------------------------------------------------
        conf.setPopulationSize(100);

        // El framework permite obtener la poblacion in
    
        Genotype Poblacion = Genotype.randomInitialGenotype(conf);
        // La Poblacion debe evolucionar para obtener resultados mas aptos
        // ---------------------------------------------------------------
        
        long TiempoComienzo = System.currentTimeMillis();
        for (int i = 0; i < MAX_EVOLUCIONES_PERMITIDAS; i++) {
            Poblacion.evolve();
            System.out.print("a");
        }
        
        long TiempoFin = System.currentTimeMillis();
        tiempoEvolucion += (TiempoFin - TiempoComienzo);
        //System.out.print("sólido6"+tiempoEvolucion);    
        // Una vez que la poblacion evoluciono es necesario obtener el cromosoma
        // mas apto para mostrarlo como solucion al problema planteado para ello
        // se utiliza el metodo getFittestChromosome
        IChromosome cromosomaMasApto;
        cromosomaMasApto = Poblacion.getFittestChromosome();
        mostrarIndividuo(cromosomaMasApto);
        /**
     *
     */       
        
    }
    
    public static void mostrarIndividuo(IChromosome individuoEncontrado){
   //genes:

        
        int Q0=(Integer)individuoEncontrado.getGene(0).getAllele();
        int R0=(Integer)individuoEncontrado.getGene(0).getAllele();
        int Q1=(Integer)individuoEncontrado.getGene(0).getAllele();
        int R1=(Integer)individuoEncontrado.getGene(0).getAllele();
        int Q2=(Integer)individuoEncontrado.getGene(0).getAllele();
        int R2=(Integer)individuoEncontrado.getGene(0).getAllele();
        int Q3=(Integer)individuoEncontrado.getGene(0).getAllele();
        int R3=(Integer)individuoEncontrado.getGene(0).getAllele();
        int Q4=(Integer)individuoEncontrado.getGene(0).getAllele();
        int R4=(Integer)individuoEncontrado.getGene(0).getAllele();
        
        System.out.print(Q0);
        System.out.print(R0);
        System.out.print(Q1);
        System.out.print(R1);
        System.out.print(Q2);
        System.out.print(R2);
        System.out.print(Q3);
        System.out.print(R3);
        System.out.print(Q4);
        System.out.print(R4);
        
        Configuration.reset();
}

     public static long getTiempoEvolucion() {
        return tiempoEvolucion;
    }

    public static void setTiempoEvolucion(long tiempoEvolucion) {
        Inventarios.tiempoEvolucion = tiempoEvolucion;
    }

    public static double getCromosomaApto() {
        return cromosomaApto;
    }

    public static void setCromosomaApto(double cromosomaApto) {
        Inventarios.cromosomaApto = cromosomaApto;
    }

    public static int getMAX_Q() {
        return MAX_Q;
    }

    public static void setMAX_Q(int MAX_Q) {
        Inventarios.MAX_Q = MAX_Q;
    }

    public static int getMAX_r() {
        return MAX_r;
    }

    public static void setMAX_r(int MAX_r) {
        Inventarios.MAX_r = MAX_r;
    }

    public int getNumarticulostemp() {
        return numarticulostemp;
    }

    public void setNumarticulostemp(int numarticulostemp) {
        this.numarticulostemp = numarticulostemp;
    }

    public double getFrecuenciapedidostemp() {
        return frecuenciapedidostemp;
    }

    public void setFrecuenciapedidostemp(double frecuenciapedidostemp) {
        this.frecuenciapedidostemp = frecuenciapedidostemp;
    }

    public double getPorcentajesatisfacciontemp() {
        return porcentajesatisfacciontemp;
    }

    public void setPorcentajesatisfacciontemp(double porcentajesatisfacciontemp) {
        this.porcentajesatisfacciontemp = porcentajesatisfacciontemp;
    }

    public double getC1temp() {
        return C1temp;
    }

    public void setC1temp(double C1temp) {
        this.C1temp = C1temp;
    }

    public double getC2temp() {
        return C2temp;
    }

    public void setC2temp(double C2temp) {
        this.C2temp = C2temp;
    }

    public double getC3temp() {
        return C3temp;
    }

    public void setC3temp(double C3temp) {
        this.C3temp = C3temp;
    }

    public double getC4temp() {
        return C4temp;
    }

    public void setC4temp(double C4temp) {
        this.C4temp = C4temp;
    }

    public double getC5temp() {
        return C5temp;
    }

    public void setC5temp(double C5temp) {
        this.C5temp = C5temp;
    }

    public int getQitemp() {
        return Qitemp;
    }

    public void setQitemp(int Qitemp) {
        this.Qitemp = Qitemp;
    }

    public double getD1temp() {
        return D1temp;
    }

    public void setD1temp(int D1temp) {
        this.D1temp = D1temp;
    }

    public double getD2temp() {
        return D2temp;
    }

    public void setD2temp(int D2temp) {
        this.D2temp = D2temp;
    }

    public double getD3temp() {
        return D3temp;
    }

    public void setD3temp(int D3temp) {
        this.D3temp = D3temp;
    }

    public double getD4temp() {
        return D4temp;
    }

    public void setD4temp(int D4temp) {
        this.D4temp = D4temp;
    }

    public double getD5temp() {
        return D5temp;
    }

    public void setD5temp(int D5temp) {
        this.D5temp = D5temp;
    }

    public int getS1temp() {
        return S1temp;
    }

    public void setS1temp(int S1temp) {
        this.S1temp = S1temp;
    }

    public double getS2temp() {
        return S2temp;
    }

    public void setS2temp(int S2temp) {
        this.S2temp = S2temp;
    }

    public double getS3temp() {
        return S3temp;
    }

    public void setS3temp(int S3temp) {
        this.S3temp = S3temp;
    }

    public double getS4temp() {
        return S4temp;
    }

    public void setS4temp(int S4temp) {
        this.S4temp = S4temp;
    }

    public double getS5temp() {
        return S5temp;
    }

    public void setS5temp(int S5temp) {
        this.S5temp = S5temp;
    }

    public int getE1temp() {
        return E1temp;
    }

    public void setE1temp(int E1temp) {
        this.E1temp = E1temp;
    }

    public int getE2temp() {
        return E2temp;
    }

    public void setE2temp(int E2temp) {
        this.E2temp = E2temp;
    }

    public int getE3temp() {
        return E3temp;
    }

    public void setE3temp(int E3temp) {
        this.E3temp = E3temp;
    }

    public int getE4temp() {
        return E4temp;
    }

    public void setE4temp(int E4temp) {
        this.E4temp = E4temp;
    }

    public int getE5temp() {
        return E5temp;
    }

    public void setE5temp(int E5temp) {
        this.E5temp = E5temp;
    }
}
